/*
 * @Descripttion:
 * @Author: YuZhenJing
 * @Date: 2020-01-01 11:48:46
 * @LastEditors: YuZhenJing
 * @LastEditTime: 2020-05-14 08:51:16
 */
import vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import singleSpaVue from 'single-spa-vue'
import elementUI from 'element-ui'
import Fragment from 'vue-fragment'
import AsmClient from 'asm-client'
import AsmAxios from './boot/asmAxios'
import './boot/plugin'

vue.use(Fragment.Plugin)
vue.use(elementUI)
vue.use(AsmClient)
vue.use(AsmAxios)

vue.prototype.bodyRootUrl = {
  asmCore: '/asmcore/',
  asmBody: '/asmbody/',
  example: '/example/'
}

vue.config.productionTip = false

const appOptions = {
  el: '#in-core',
  render: h => h(App),
  router,
  store
}

if (process.env.VUE_APP_SINGLERUN === 'true') {
  // eslint-disable-next-line no-unused-expressions
  import('asm-client/lib/asm-style/reset/index.css')
  // eslint-disable-next-line no-unused-expressions
  import('asm-client/lib/asm-style/line-awesome/css/line-awesome.min.css')
  // eslint-disable-next-line no-unused-expressions
  import('element-ui/lib/theme-chalk/index.css')

  const whiteList = ['/404', '/403'] // 设置白名单，避免死循环
  router.beforeEach(async (to, from, next) => {
    const _this = new vue()
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      let param = {
        authId: 'session:single:login',
        username: 'super',
        platformTypeCode: 'pc_fun_code'
      }
      await _this.$auth.clearCache.all()
      await _this.$html
        .post('/asmcore/auth/session/singleLogin', param, {
          root: true
        })
        .then(data => {
          const resultData = data.returnData
          _this.$auth.hostUrl.set(resultData.hostUrl)
          _this.$auth.menuInfo.set(resultData.menuList)
          _this.$auth.token.set(resultData.tokenValue)
          _this.$auth.userInfo.set(resultData.userInfo)
          next()
        })
        .catch(() => {
          next({
            path: '/404'
          })
        })
    }
  })

  AsmClient.Colors.setColors(AsmClient.initParam.htmlParam.defaultTheme)

  new vue(appOptions)
}

const vueLifecycles = singleSpaVue({
  Vue: vue,
  appOptions: appOptions
})

export const bootstrap = vueLifecycles.bootstrap
export const mount = vueLifecycles.mount
export const unmount = vueLifecycles.unmount
